package cn.liyunqi.ttv.web.controller;


import cn.liyunqi.core.exception.BizException;
import cn.liyunqi.ttv.web.common.RequestMap;
import cn.liyunqi.ttv.web.common.ResponseMap;
import cn.liyunqi.ttv.web.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private CommonService commonService;

    /**
     * 获取邮箱注册验证嘛
     * @return
     */
    @RequestMapping(value = "/getApiParam",method = RequestMethod.GET)
    public @ResponseBody
    Object getMail(RequestMap requestMap){
        ResponseMap<String,Object> responseMap = new ResponseMap<String,Object>();
        try {

            return commonService.getApiParam(responseMap);
        } catch (BizException e){
            responseMap.returnError(e);
            //e.printStackTrace();
        } catch (Exception e){
            responseMap.returnError();
            e.printStackTrace();
        }
        return responseMap;
    }
    /**
     * 获取版本信息
     * @return
     */
    @RequestMapping(value = "/getVersion",method = RequestMethod.GET)
    public @ResponseBody
    Object getVersion(RequestMap requestMap){
        ResponseMap<String,Object> responseMap = new ResponseMap<String,Object>();
        try {

            return commonService.getVersion(responseMap);
        } catch (BizException e){
            responseMap.returnError(e);
            //e.printStackTrace();
        } catch (Exception e){
            responseMap.returnError();
            e.printStackTrace();
        }
        return responseMap;
    }
    /**
     * 获取版本信息
     * @return
     */
    @RequestMapping(value = "/checkText",method = RequestMethod.POST)
    public @ResponseBody
    Object checkText(RequestMap requestMap){
        ResponseMap<String,Object> responseMap = new ResponseMap<String,Object>();

        return responseMap;
    }

    /**
     * 获取邮箱注册验证嘛
     * @return
     */
    @RequestMapping(value = "/checkWechatAuth",method = RequestMethod.GET)
    public HttpServletResponse checkWechatAuth(HttpServletResponse response,RequestMap requestMap){
        try {
            //signature	微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
            String signature =  requestMap.getStringValue("signature","signature",false);
            //timestamp	时间戳
            String timestamp =  requestMap.getStringValue("timestamp","timestamp",false);
            //nonce	随机数
            String nonce =  requestMap.getStringValue("nonce","nonce",false);
            //echostr	随机字符串
            String echostr = requestMap.getStringValue("echostr","echostr",false);

            String token = "";

            response.getOutputStream().write(echostr.getBytes());
        } catch (BizException e){
           return null;
            //e.printStackTrace();
        } catch (Exception e){
            return null;
        }
        return null;
    }
}
